require 'View/UIBase/UIBase'

UIGuildInvestment = UIBase.New(PanelResNames.UIGuildInvestment)

function UIGuildInvestment.Init(func)
    UIBase.Init(UIGuildInvestment,func);
end

-- 创建回调
function UIGuildInvestment:OnCreate()
    self.content = self:SetDynamicList(self:GetChild("Com_Iteminformation"),self.updateItem, nil, true)
    self.Text_Time = self:GetChild("Text_time")
    self.Text_RefreshTime = self:GetChild("Text_refreshtime")
    self.Text_availablecredit = self:GetChild("Text_availablecredit")
    self.Text_availablecredit1 = self:GetChild("Text_availablecredit1");
    self.Icon_3 = self:GetChild("Icon_3");
    self.Img_guildcoin = self:GetChild("Img_guildcoin");
    self.Text_guildcoin = self:GetChild("Text_guildcoin");
    self:GetChild("Text_mypurchase").text = getLanguage("GuildInvestment_1004")
    self:GetChild("Text_helppeoplebargain").text = getLanguage("GuildInvestment_1005")
    self:GetChild("Text_guildshop").text = getLanguage("GuildInvestment_1001")
    self:GetChild("Text_name").text = getLanguage("GuildInvestment_1011")
    --self:GetChild("Text_price").text = getLanguage("GuildInvestment_1012")
    self:GetChild("Text_memberbargaining").text = getLanguage("GuildInvestment_1013")
    self:GetChild("Text_holdings").text = getLanguage("GuildInvestment_1014")
    self:GetChild("Text_averagecost").text = getLanguage("GuildInvestment_1015")
    self:GetChild("Text_title1").text = getLanguage("GuildInvestment_1143")
    local openTime = logicMgr.GuildManager.getGuildParaIntValue(4) / 60 / 60;
    local endTime = logicMgr.GuildManager.getGuildParaIntValue(5) / 60 / 60;
    self:GetChild("Text_eventopeningdate").text = string.format(getLanguage("GuildInvestment_1009"), openTime, endTime);
    self.stateController = self:GetController("state");
    self:addEvent();
end

-- 显示
function UIGuildInvestment:OnEnable()
    self.curIndex = 1;
    unionMarketHandler.reqGuildSelfMarketInfo()
end

function UIGuildInvestment:addEvent()
    self:RegisterOnClick(self:GetChild("BtnBack"),self.OnClose);
    self:RegisterOnClick(self:GetChild("Btn_mypurchase"),self.OnMyPurchase);
    self:RegisterOnClick(self:GetChild("Btn_helppeoplebargain"),self.OnHelpPeopleBargain);
    self:RegisterOnClick(self:GetChild("Btn_sellout"),self.OnSellOut);
    self:RegisterOnClick(self:GetChild("Btn_purchase"),self.OnSellIn);
    self:RegisterOnClick(self:GetChild("Btn_tips"),self.OnShowHelp);
    self.content.onClickItem:Add(self.OnClickItem, self);
    self:RegisterEvent(Config.EventType.Fresh_GuildMarket,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_Currency,self.Refresh);
end

function UIGuildInvestment:removeEvent()
    self.content.scrollPane.onScrollEnd:Clear();
    self.content.onClickItem:Clear();
end

function UIGuildInvestment:Refresh()
    self.state, self.countDownAll = logicMgr.GuildManager.getGuildMarketStateAndTime();
    if self.state == 1 then
        self.stateController.selectedIndex = 1;
    elseif self.state == 2 then
        self.stateController.selectedIndex = 0;
        self:UpdateData();
    else
        self.stateController.selectedIndex = 2;
        self:UpdateData();
    end
    self:updataTime();
    self:UpdateCurrency();
end

function UIGuildInvestment:UpdateData()
    self.marketList = logicMgr.GuildManager.getGuildMarketSellNodeInfo();
    self.costNum = logicMgr.ItemManager.getCurrencyCountByModelId(logicMgr.GuildManager._GuildMarketCost);
    for k, v in pairs(self.marketList) do
        v.upDownNum = math.floor((v.currentPrice - v.lastPrice) / v.lastPrice * 10000);
        v.canBuy = self.costNum >= v.currentPrice and 1 or 0;
    end
    table.sort(self.marketList, function (a, b)
        --if a.canBuy ~= b.canBuy then return a.canBuy > b.canBuy end;
        return a.upDownNum > b.upDownNum;
    end)
    self.content.numItems = #self.marketList;
    local use = HelperFunc.FormatResourceNum(logicMgr.GuildManager.getGuildMarketUseQuota());
    local limit = HelperFunc.FormatResourceNum(logicMgr.GuildManager.getGuildMarketUseLimit());
    self.Text_availablecredit.text = getLanguage("GuildInvestment_1008");
    self.Icon_3.url = EnumConst.AtlasPath.ItemAtlas..logicMgr.ItemManager.GetItemIcon(logicMgr.GuildManager._GuildMarketCost);
    self.Text_availablecredit1.text = string.format("%s/%s", use, limit);

end

function UIGuildInvestment:updateItem(index, itemObj)
    local serverData = self.marketList[index];
    itemObj.visible = true;
    local Text_name = itemObj:GetChild("Text_name");
    local Text_price = itemObj:GetChild("Text_price");
    local Text_memberbargaining = itemObj:GetChild("Text_memberbargaining");
    local Text_memberbargaining1 = itemObj:GetChild("Text_memberbargaining1");
    local Text_holdings = itemObj:GetChild("Text_holdings");
    local icon = itemObj:GetChild("icon");
    local Icon_1 = itemObj:GetChild("Icon_1");
    local Icon_2 = itemObj:GetChild("Icon_2");
    local Icon_3 = itemObj:GetChild("Icon_3");
    Icon_1.url = EnumConst.AtlasPath.ItemAtlas..logicMgr.ItemManager.GetItemIcon(logicMgr.GuildManager._GuildMarketCost);
    Icon_2.url = EnumConst.AtlasPath.ItemAtlas..logicMgr.ItemManager.GetItemIcon(logicMgr.GuildManager._GuildMarketCost);
    Icon_3.url = EnumConst.AtlasPath.ItemAtlas..logicMgr.ItemManager.GetItemIcon(logicMgr.GuildManager._GuildMarketCost);
    icon.url = logicMgr.GuildManager.getGuildMarketSellIconUrlById(serverData.itemID);
    local Text_losemoney = itemObj:GetChild("Text_losemoney");
    local upanddownController = itemObj:GetController("upanddown");
    local BargainingController = itemObj:GetController("Bargaining");
    BargainingController.selectedIndex = serverData.apply;
    Text_name.text = logicMgr.GuildManager.getGuildMarketSellNameById(serverData.itemID);
    Text_price.text = serverData.currentPrice;
    Text_memberbargaining1.text = string.format("(%s%%)", serverData.upDownNum * 0.01);
    Text_memberbargaining.text = math.abs(serverData.lastPrice - serverData.currentPrice);
    Text_holdings.text = serverData.possessNum;
    Text_losemoney.text = serverData.averagePrice;
    if serverData.lastPrice < serverData.currentPrice then
        upanddownController.selectedIndex = 0;
    elseif serverData.lastPrice == serverData.currentPrice then
        upanddownController.selectedIndex = 3;
    else
        upanddownController.selectedIndex = 1;
    end
    if serverData.lastPrice == 0 then
        upanddownController.selectedIndex = 2;
    end
    if self.curIndex == index then
        itemObj.selected = true;
    else
        itemObj.selected = false;
    end
end

function UIGuildInvestment:OnClickItem(context)
    self.curIndex = self.content:GetChildIndex(context.data) + 1;
end

function UIGuildInvestment:updataTime()
    if self.state == 1 then
        self.Text_RefreshTime.text = getLanguage("GuildInvestment_1017");
    elseif self.state == 4 then
        self.Text_RefreshTime.text = getLanguage("GuildInvestment_1018");
    elseif self.state == 3 then
        self.Text_RefreshTime.text = getLanguage("GuildInvestment_1010");
    else
        self.Text_RefreshTime.text = getLanguage("GuildInvestment_1016");
    end
    self.countDown = math.floor(self.countDownAll - HelperFunc.GetServerTime());
    self.Text_Time.text = HelperFunc.GetTimeStr(self.countDown);
    if self.Time ~= nil then
        utimer.remove(self.Time);
        self.Time = nil;
    end
    self.Time = self:TimeCall(self.OnCountDown, 1000, -1);
end

function UIGuildInvestment:OnCountDown()
    self.countDown = self.countDown - 1;
    self.Text_Time.text = HelperFunc.GetTimeStr(self.countDown);
    if self.countDown <= 0 then
        unionMarketHandler.reqGuildSelfMarketInfo()
        return true;
    end
end

function UIGuildInvestment:UpdateCurrency()
    self.Img_guildcoin.url = EnumConst.AtlasPath.ItemAtlas .. logicMgr.ItemManager.GetItemIcon(logicMgr.GuildManager._GuildMarketCost);
    self.Text_guildcoin.text = HelperFunc.FormatResourceNum(logicMgr.ItemManager.getBagNumByTableID(logicMgr.GuildManager._GuildMarketCost));
end

---卖出
function UIGuildInvestment:OnSellOut()
    if self.curIndex == nil then
        return ;
    end
    if self.marketList[self.curIndex].possessNum > 0 then
        showUI(PanelNames.GuildMarketSellTips, nil, 1, self.marketList[self.curIndex].itemID);
    else
        UISysTips.AddMsg(getLanguage("GuildInvestment_1134"));
    end
end

---买入
function UIGuildInvestment:OnSellIn()
    if self.curIndex == nil then
        return ;
    end
    if logicMgr.ItemManager.getCurrencyCountByModelId(logicMgr.GuildManager._GuildMarketCost) >= self.marketList[self.curIndex].currentPrice then
        if logicMgr.GuildManager.getGuildMarketUseLimit() - logicMgr.GuildManager.getGuildMarketUseQuota() >= self.marketList[self.curIndex].currentPrice then
            showUI(PanelNames.GuildMarketSellTips, nil, 0, self.marketList[self.curIndex].itemID);
        else
            UISysTips.AddMsg(getLanguage("GuildInvestment_1133"));
        end
    else
        UISysTips.AddMsg(getLanguage("GuildInvestment_1132"));
    end
end

---帮人砍价
function UIGuildInvestment:OnHelpPeopleBargain()
    showUI(PanelNames.GuildMarketBargainingTips);
end

---我的购买
function UIGuildInvestment:OnMyPurchase()
    showUI(PanelNames.GuildMarketMyPurchaseTips);
end

---提示说明
function UIGuildInvestment:OnShowHelp()
    logicMgr.UIShowManager.ShowCommonTips("GuildInvestment_1115", "GuildInvestment_1113");
end

-- 关闭
function UIGuildInvestment:OnClose()
    self:OnBack();
    self:RemoveAllTimeCall();
end

-- 隐藏
function UIGuildInvestment:OnDisable()
end

-- 卸载
function UIGuildInvestment:OnDestroy()
    self:removeEvent();
end

return UIGuildInvestment